package com.yc.hr.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.yc.hr.biz.BizException;
import com.yc.hr.biz.DeptBiz;
import com.yc.hr.util.UIUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class DeptEditDlg extends MyDIalog {
	private static final long serialVersionUID = 1L;
//	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			DeptEditDlg dialog = new DeptEditDlg(null, null);
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	private JButton cancelButton;
	private JButton okButton;
	
	// 业务对象
	private DeptBiz biz = new DeptBiz();
	
	// 当点击ok 保存成功之后执行的任务对象
//	private Runnable okTask;

	/**
	 * Create the dialog.
	 */
	// okTask 是当点击ok 保存成功之后执行的任务对象
	public DeptEditDlg(Runnable okTask, Map<String,Object> dept) {
//		this.okTask = okTask;
		setModal(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("部门编号");
		lblNewLabel.setBounds(83, 43, 54, 15);
		contentPanel.add(lblNewLabel);
		{
			textField = new JTextField();
			textField.setEditable(false);
			textField.setBounds(150, 40, 189, 21);
			contentPanel.add(textField);
			textField.setColumns(10);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("部门名称");
			lblNewLabel_1.setBounds(83, 88, 54, 15);
			contentPanel.add(lblNewLabel_1);
		}
		{
			textField_1 = new JTextField();
			textField_1.setColumns(10);
			textField_1.setBounds(150, 85, 189, 21);
			contentPanel.add(textField_1);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("部门位置");
			lblNewLabel_1.setBounds(83, 132, 54, 15);
			contentPanel.add(lblNewLabel_1);
		}
		{
			textField_2 = new JTextField();
			textField_2.setColumns(10);
			textField_2.setBounds(150, 129, 189, 21);
			contentPanel.add(textField_2);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("保存");
				
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("退出");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		///////////////////////////////////////////////
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeptEditDlg.this.dispose();
			}
		});
		
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(textField.getText().isBlank() == true) {
						biz.create(textField_1.getText(), textField_2.getText());
					} else {
						// 保存之前, 将界面中编辑的值写回到 map 集合中
						dept.put("DEPTNO", textField.getText());
						dept.put("DNAME", textField_1.getText());
						dept.put("LOC", textField_2.getText());
						biz.modify(dept);
					}
					DeptEditDlg.this.dispose();
					// 执行保存之后的任务方法
					okTask.run();
				} catch (BizException e1) {
//					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
			}
		});
		
		// 判断传入的 dept 对象, 如果不为空, 则表示当前窗口是在修改某个部门
		if(dept != null) {
			textField.setText(dept.get("DEPTNO").toString());
			textField_1.setText(dept.get("DNAME").toString());
			textField_2.setText(dept.get("LOC").toString());
		}
		
		// 设置窗口居中
		UIUtils.setCenter(this);
		
	}

}
